package com.spring.sunflower.login;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.common.WebViewActivity;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.util.AppUtils;


/**
 * Created by Kevin on 2021/5/21<br/>
 * <p>
 * Describe:<br/>
 */
public class LoginByPhoneActivity2 extends BaseActivity<LoginPresenter> implements View.OnClickListener, LoginContract.View {
    private LinearLayout llNextStepContainer;
    private EditText etPhoneNum;
    private CheckBox checkBox;
    private TextView tvAgreement, tvPrivateAgreement;
    private TextView tvNext;

    @Override
    public void initView() {
        llNextStepContainer = findViewById(R.id.ll_next_step_container);
        llNextStepContainer.setOnClickListener(this);
        tvNext = findViewById(R.id.tvNext);
        etPhoneNum = findViewById(R.id.etPhoneNum);
        checkBox = findViewById(R.id.checkBox);
        tvAgreement = findViewById(R.id.tvAgreement);
        tvPrivateAgreement = findViewById(R.id.tvPrivateAgreement);
        tvAgreement.setOnClickListener(this::onClick);
        tvPrivateAgreement.setOnClickListener(this::onClick);
        etPhoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                printD("s=" + s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                printD("sss=" + s.toString());
                if (s.toString().length() == 11) {
//                    llNextStepContainer.setBackground(getResources().getDrawable(R.drawable.ic_btn_bg_white));
                    tvNext.setTextColor(getColorX(R.color.white));
                } else {
//                    llNextStepContainer.setBackground(getResources().getDrawable(R.drawable.ic_btn_bg_white_alpha));
                    tvNext.setTextColor(getColorX(R.color.white_alpha_50));
                }
            }
        });
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_login_by_phone_2;
    }

    @Override
    public LoginPresenter setPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void onClick(View v) {
        if (isFastClick()) return;
        switch (v.getId()) {
            case R.id.ll_next_step_container:
                String phoneNum = etPhoneNum.getText().toString().trim();
                if (TextUtils.isEmpty(phoneNum)) {
                    showToast("手机号不能为空");
                    return;
                }
                if (!checkBox.isChecked()) {
                    showToast("请勾选同意协议");
                    return;
                }
                if (phoneNum.length() != 11 || !AppUtils.isValidPhoneNum(phoneNum)) {
                    showToast("请输入正确手机号");
                } else {
//                    mPresenter.requestAuthCode(phoneNum, "0");
                    Intent intent = new Intent(this, LoginByAuthCodeActivity.class);
                    intent.putExtra("phoneNum", phoneNum);
                    startActivity(intent);
                }
                break;
            case R.id.tvAgreement:
                goAgreement();
                break;
            case R.id.tvPrivateAgreement:
                goPrivateAgreement();
                break;
        }
    }

    @Override
    public void requestFinish() {
        super.requestFinish();
        Intent intent = new Intent(this, LoginByAuthCodeActivity.class);
        intent.putExtra("phoneNum", etPhoneNum.getText().toString().trim());
        startActivity(intent);
    }

    public void goAgreement() {
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtra(Constant.KEY_WEB_VIEW_TITLE, "用户协议");
        intent.putExtra(Constant.KEY_WEB_VIEW_URL, Constant.REGISTER_AGREEMENT);
        startActivity(intent);
    }

    public void goPrivateAgreement() {
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtra(Constant.KEY_WEB_VIEW_TITLE, "隐私协议");
        intent.putExtra(Constant.KEY_WEB_VIEW_URL, Constant.PRIVATE_AGREEMENT);
        startActivity(intent);
    }
}
